package com.guoxue.action.back;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.guoxue.action.front.BaseAction;
import com.guoxue.domain.IOSDeviceVo;
import com.guoxue.domain.PushVo;
import com.guoxue.model.PushMessageVo;
import com.guoxue.service.IIOSDeviceService;
import com.guoxue.service.IPushService;
import com.guoxue.service.IUserService;
import com.guoxue.util.LoggerUtil;
import com.guoxue.util.OtherUtils;
import com.guoxue.util.PushUtil;
import com.guoxue.util.RedisUtil;

import redis.clients.jedis.Jedis;


@Controller
@RequestMapping(value="/push")
public class PushAction extends BaseAction{
	
	@Resource
	private IUserService userService;
	@Resource
	private IPushService pushService;
	@Resource
	private PushUtil pushUtil;
	@Resource
	private IIOSDeviceService iOSDeviceService;
	
	/**
	 * 添加推送消息
	 * @param req
	 * @param resp
	 * @param pub
	 * @return
	 */
	@RequestMapping("/pushAddMessage")
	@ResponseBody
	public JSON pushAddMessage(HttpServletRequest req, HttpServletResponse resp, PushVo push){
		try {
			if(isEmpty(push.getContent())){
				return Message(ERROR, "请填写推送消息内容", null);
			}
			if(isEmpty(push.getTitle())){
				return Message(ERROR, "请填写推送消息标题", null);
			}
			push.setCreateTime(OtherUtils.getCurrentDate());
			int id = push.getId();
			pushService.savePush(push);
			if(null != Integer.valueOf(id)){
				return Message(SUCCESS, "推送消息修改成功", null);
			}else{
				return Message(SUCCESS, "推送消息添加成功", null);
			}
		} catch (Exception e) {
			return LoggerUtil.logInfo(e,"推送消息添加异常");
		}
	}
	/**
	 * 推送消息
	 * @param req
	 * @param resp
	 * @param id 消息id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/pushMessage")
	public JSON pushMessage(HttpServletRequest req, HttpServletResponse resp,Integer id){
		try{
			if(null == id){
				return Message(ERROR, "推送消息id不能为空", null);
			}
			PushVo push =  pushService.getPush(id);
			String message = push.getContent();
			PushMessageVo pushMessage = new PushMessageVo();
			if(message.contains("http") || message.contains("www") || message.contains(".com") || message.contains(".cn")||message.contains("https")){
				  String regEx = "[\\u4e00-\\u9fa5]";  
			 	  Pattern p = Pattern.compile(regEx);  
			 	  Matcher m = p.matcher(message);  
				if(m.find()){
					pushMessage.setType("TEXTANDLINK");
				}else{
					pushMessage.setType("LINK");
				}
			}else{
				pushMessage.setType("TEXT");
			}
			pushMessage.setContent(message);
			Calendar c = Calendar.getInstance();
			c.setTime(new Date());
	        c.add(Calendar.DAY_OF_MONTH, 30);
			Jedis jedis = RedisUtil.getJedis();
			jedis.set((id+"Push").getBytes(), RedisUtil.serialize(pushMessage));
			jedis.expireAt((id+"Push").getBytes(), c.getTime().getTime()/1000);
			jedis.close();
			pushMessage.setContent(message);
			BaseAction.PushMessageVo = pushMessage;
			List<IOSDeviceVo> IOSDeviceVoList = iOSDeviceService.getIOSDeviceList(1);
			if(IOSDeviceVoList.size() > 0){
				List<String> IOSDeviceList = new ArrayList<String>();
				for(IOSDeviceVo i :IOSDeviceVoList){
					if(null != i.getDeviceToken()){
						IOSDeviceList.add(i.getDeviceToken());
					}
				}
				if(IOSDeviceVoList.size() > 0){
					pushUtil.sendPush(IOSDeviceList, message, false, null);
				}
			}
			push.setSysflag(1);
			pushService.savePush(push);
			return Message(SUCCESS, "推送消息成功", null);
		}catch(Exception e){
			return LoggerUtil.logInfo(e, "推送消息异常");
		}
	}
	
}
